package tech.wetech.admin3.chat.controller;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import tech.wetech.admin3.chat.dto.MessageDto;

import java.util.List;

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class ChatController {

  // 智能对话客户端
  private final ChatClient chatClient;

  @GetMapping("/chat")
  public ResponseEntity<MessageDto> chat(@RequestParam(value = "message", defaultValue = "你好!") String message) {
    MessageDto messageDto = new MessageDto(this.chatClient.prompt()
      .user(message)
      .call()
      .content());
    return new ResponseEntity<>(messageDto, HttpStatus.OK);
  }

  @GetMapping("/stream/chat")
  public ResponseEntity<Flux<String>> streamChat(@RequestParam(value = "message", defaultValue = "你好!") String message) {
    Flux<String> flux = this.chatClient.prompt()
     .user(message)
     .stream()
     .content();
    return new ResponseEntity<>(flux, HttpStatus.OK);
  }

}
